<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model.trim="query.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="keyword"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="deptRef"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          node-key="id"
          :expand-on-click-node="false"
          highlight-current
          @current-change="setNode"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="importExcel">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{row}">
              <span>{{ row.formOfEmployment===1? '正式':'非正式' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{row}">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="openEmployee(row.id)">角色</el-button>
              <el-button size="mini" type="text" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            :current-page="query.page"
            :page-size="query.pagesize"
            @current-change="handleCurrentChange"
          />
        </el-row>
        <!-- 分页 -->
      </div>
    </div>
    <import-excel :show-excel-dialog.sync="showExcelDialog" @uploadSuccess="getEmployee" />

    <el-dialog :visible="showProdialog" title="分配角色" @close="close">
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in roleIdsList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>

      <el-row type="flex" justify="center">
        <el-col :span="4">
          <el-button type="primary" size="mini" @click="btnOk">确定</el-button>
          <el-button size="mini" @click="close">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { getEmployee, delEmployee, exportEmployee, updateAssignRoles, getEmployeeDefailt } from '@/api/employee'
import { enabledRole } from '@/api/role'
import { transListToTreeData } from '@/utils'
import fileSaver from 'file-saver'
import importExcel from './components/import-excel.vue'
export default {
  name: 'Employee',
  components: { importExcel },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      query: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      list: [],
      total: 0,
      showExcelDialog: false,
      enabledRoleList: [],
      showProdialog: false,
      roleIdsList: [],
      roleIds: [],
      EmployeeId: null
    }
  },
  async created() {
    this.loadDepartment()
    this.enabledRole()
  },
  methods: {
    async loadDepartment() {
      const res = await getDepartment()
      this.depts = transListToTreeData(res.data, 0)
      this.query.departmentId = this.depts[0].id
      this.$nextTick(() => {
        this.$refs.deptRef.setCurrentKey(this.query.departmentId)
      })
      this.getEmployee()
    },
    setNode(e) {
      this.query.departmentId = e.id
      this.query.page = 1
      this.getEmployee()
    },
    async getEmployee() {
      const res = await getEmployee(this.query)
      this.list = res.data.rows
      this.total = res.data.total
    },
    async enabledRole() {
      const res = await enabledRole()
      this.roleIdsList = res.data
    },
    handleCurrentChange(e) {
      this.query.page = e
      this.getEmployee()
    },
    keyword() {
      clearTimeout(this.timer)
      this.timer = setTimeout(async() => {
        this.query.page = 1
        await this.getEmployee()
      }, 500)
    },
    delEmployee(id) {
      this.$confirm('确认要删除吗?').then(async() => {
        const res = await delEmployee(id)
        this.$message.success(res.message)
        this.query.departmentId = id
        this.loadDepartment()
      }).catch(() => {})
    },
    async exportEmployee() {
      const res = await exportEmployee()
      fileSaver.saveAs(res, '员工信息表.xlsx')
    },
    importExcel() {
      this.showExcelDialog = true
    },
    async openEmployee(id) {
      this.showProdialog = true
      this.EmployeeId = id
      const res = await getEmployeeDefailt(id)
      this.roleIds = res.data.roleIds
    },
    async btnOk() {
      const res = await updateAssignRoles({ roleIds: this.roleIds, id: this.EmployeeId })
      this.$message.success(res.message)
      this.close()
    },
    close() {
      this.showProdialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin: 10px;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04c9be;
      font-size: 12px;
      display: inline-block;
    }
  }
}
.el-dialog__title {
    line-height: 24px;
    font-size: 18px;
    color: #303133;
}
</style>
